import json
from fastapi import WebSocket

from fastapi import FastAPI
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
import time
#后台定时任务处理队列中的图片URL，调用百度OCR进行识别
from tools.bdapi import bdAPI
from tools.myredis import r
from tools.webscoket import manager
scheduler = BackgroundScheduler()

def print_current_time():
    if r.llen('idcard')>0:
        ilist = r.lrange('idcard', 0, 10)
        print("print_current_time执行中")
        # print(ilist)
        for i in ilist:
            i = json.loads(i)
            # i = {"user_id": 1, "idcard_url": "123"}
            user_id = i.get("user_id")
            url = i.get("url")
            # print(i)

            mes = bdAPI.idcard(url)
            # mes已经是JSON字符串格式，直接发送即可
            print(f"处理用户{user_id}的身份证识别，结果: {mes}")
            # 确保user_id是字符串类型，mes已经是JSON字符串，直接发送
            manager.send_message(str(user_id), mes)

        r.ltrim('idcard', 10, -1)



def add_jobs():#定时任务
    scheduler.add_job(
        func=print_current_time,
        trigger="interval",
        seconds=1,
        id="time_printer",
    )


